#include "sessiondetailwidget.h"
#include "mainwidget.h"

///////////////////////////////////////////////
// 表示一个头像 + 一个名字组合的控件
///////////////////////////////////////////////
AvatarItem::AvatarItem(const QIcon &avatar, const QString &name, bool isAdd)
{
    // 设置自身属性
    this->setFixedSize(70, 80);

    // 创建布局管理器
    QVBoxLayout *layout = new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setAlignment(Qt::AlignCenter);
    this->setLayout(layout);

    // 创建头像
    _avatarBtn = new QPushButton();
    _avatarBtn->setFixedSize(45, 45);
    _avatarBtn->setIconSize(QSize(45, 45));
    _avatarBtn->setIcon(avatar);
    QString style = "QPushButton { border: none; background-color: transparent; }";
    if (isAdd)
        style += "QPushButton:pressed { background-color: rgb(235, 235, 235); }";
    _avatarBtn->setStyleSheet(style);

    // 创建昵称
    _nameLabel = new QLabel();
    _nameLabel->setText(name);
    QFont font("微软雅黑", 10);
    _nameLabel->setFont(font);
    _nameLabel->setAlignment(Qt::AlignCenter);

    // 对昵称作截断
    const int MAX_WIDTH = 65;
    QFontMetrics metrics(font);
    int totalWidth = metrics.horizontalAdvance(name);
    if (totalWidth > MAX_WIDTH)
    {
        QString tail = "...";
        int tailWidth = metrics.horizontalAdvance(tail);
        int availableWidth = MAX_WIDTH - tailWidth;
        int availableSize = name.size() * ((double)availableWidth / totalWidth);
        QString newName = name.left(availableSize);
        _nameLabel->setText(newName + tail);
    }

    layout->addWidget(_avatarBtn);
    layout->addWidget(_nameLabel);
}


AvatarItem::AvatarItem(const UserInfo &userInfo, bool isAdd)
    :_userInfo(userInfo)
{
    // 设置自身属性
    this->setFixedSize(70, 80);

    // 创建布局管理器
    QVBoxLayout *layout = new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setAlignment(Qt::AlignCenter);
    this->setLayout(layout);

    // 创建头像
    _avatarBtn = new QPushButton();
    _avatarBtn->setFixedSize(45, 45);
    _avatarBtn->setIconSize(QSize(45, 45));
    _avatarBtn->setIcon(_userInfo._avatar);
    QString style = "QPushButton { border: none; background-color: transparent; }";
    if (isAdd)
        style += "QPushButton:pressed { background-color: rgb(235, 235, 235); }";
    _avatarBtn->setStyleSheet(style);

    // 创建昵称
    _nameLabel = new QLabel();
    _nameLabel->setText(_userInfo._nickname);
    QFont font("微软雅黑", 10);
    _nameLabel->setFont(font);
    _nameLabel->setAlignment(Qt::AlignCenter);

    // 对昵称作截断
    const int MAX_WIDTH = 65;
    QFontMetrics metrics(font);
    int totalWidth = metrics.horizontalAdvance(_userInfo._nickname);
    if (totalWidth > MAX_WIDTH)
    {
        QString tail = "...";
        int tailWidth = metrics.horizontalAdvance(tail);
        int availableWidth = MAX_WIDTH - tailWidth;
        int availableSize = _userInfo._nickname.size() * ((double)availableWidth / totalWidth);
        QString newName = _userInfo._nickname.left(availableSize);
        _nameLabel->setText(newName + tail);
    }

    layout->addWidget(_avatarBtn);
    layout->addWidget(_nameLabel);

    if (!isAdd)
    {
        connect(_avatarBtn, &QPushButton::clicked, this, [=](){
            MainWidget *mainWidget = MainWidget::getInstance();
            UserInfoWidget *userInfoWidget = new UserInfoWidget(_userInfo, mainWidget);
            userInfoWidget->exec();
        });
    }
}

///////////////////////////////////////////////
// 表示单聊会话详情的窗口
///////////////////////////////////////////////
SessionDetailWidget::SessionDetailWidget(QWidget *parent)
    :QDialog(parent)
{
    // 设置基本属性
    this->setWindowTitle("会话详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setFixedSize(300, 300);
    this->setStyleSheet("QDialog { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 创建布局管理器
    QGridLayout *layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(20, 5, 20, 5);
    this->setLayout(layout);

    // // 添加创建群聊按钮
    // AvatarItem *createGroupBtn = new AvatarItem(QIcon(":/resource/image/cross.png"), "添加", true);
    // layout->addWidget(createGroupBtn, 0, 0);

    // 添加当前用户的信息
#if TEST_UI
    UserInfo userInfo;
    userInfo._user_id = "111";
    userInfo._mail = "222222222";
    userInfo._nickname = "阿吉";
    userInfo._avatar = QIcon(":/resource/image/defaultAvatar.png");
    AvatarItem *currentUser = new AvatarItem(userInfo);
    layout->addWidget(currentUser, 0, 1);
#endif

    // 添加删除好友按钮
    _deleteFriendBtn = new QPushButton();
    _deleteFriendBtn->setFixedHeight(50);
    _deleteFriendBtn->setText("删除好友");
    QString style = "QPushButton { border: 1px solid rgb(90, 90, 90); border-radius: 5px; background-color: transparent; }";
    style += "QPushButton:pressed { background-color: rgb(235, 235, 235); }";
    _deleteFriendBtn->setStyleSheet(style);
    layout->addWidget(_deleteFriendBtn, 1, 0, 1, 3);

    // connect(createGroupBtn->getAvatarBtn(), &QPushButton::clicked, this, [=](){
    //     ChooseFriendWidget *chooseFriendWidget = new ChooseFriendWidget(this);
    //     chooseFriendWidget->exec();
    // });
}

